/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swing.formularios;

import hibernate.entidades.Trabajadores;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.hibernate.exception.DataException;
import swing.InterfazPrincipal;
import swing.modelos.ModeloTrabajadores;

/**
 *
 * @author ariel
 */
public class FormTrabajador extends javax.swing.JDialog {

    ModeloTrabajadores modeloTrabajadores;
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("swing.formularios/Bundle");

    /**
     * Creates new form formClientes
     */
    public FormTrabajador(ModeloTrabajadores modeloTrabajadores, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.modeloTrabajadores = modeloTrabajadores;
        initComponents();
        getRootPane().setDefaultButton(botonNew);
    }

    public FormTrabajador(ModeloTrabajadores modeloTrabajadores, InterfazPrincipal aThis, boolean b, int fila) {
        this(modeloTrabajadores, aThis, b);
        this.campoDni.setEditable(false);
        Trabajadores trabajador = modeloTrabajadores.getRow(fila);
        this.campoDni.setText(trabajador.getCodTrab());
        this.campoNombre.setText(trabajador.getNombre());
        this.campoSalario.setText(String.valueOf(trabajador.getSalario()));
        this.campoDate.setDate(trabajador.getFechaContrato());
        this.campoTipoContrato.setText(trabajador.getTipoContrato());
        botonNew.setText(bundle.getString("textoBotonEditar"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPrincipal = new javax.swing.JPanel();
        panelFormulario = new javax.swing.JPanel();
        campoDni = new javax.swing.JTextField();
        etiquetaDni = new javax.swing.JLabel();
        etiquetaNombre = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        etiquetaFcontrato = new javax.swing.JLabel();
        etiquetaTipoContrato = new javax.swing.JLabel();
        campoTipoContrato = new javax.swing.JTextField();
        etiquetaSalario = new javax.swing.JLabel();
        campoSalario = new javax.swing.JTextField();
        campoDate = new com.toedter.calendar.JDateChooser();
        botonNew = new javax.swing.JButton();
        botonCanel = new javax.swing.JButton();
        botonDelete = new javax.swing.JButton();
        etiquetaTitulo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("swing/formularios/Bundle"); // NOI18N
        panelFormulario.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("FormTrabajador.panelFormulario.border.title"))); // NOI18N

        etiquetaDni.setText(bundle.getString("DNI")); // NOI18N

        etiquetaNombre.setText(bundle.getString("NOMBRE")); // NOI18N

        etiquetaFcontrato.setText(bundle.getString("f_contrato")); // NOI18N

        etiquetaTipoContrato.setText(bundle.getString("t_contrato")); // NOI18N

        etiquetaSalario.setText(bundle.getString("salario")); // NOI18N

        javax.swing.GroupLayout panelFormularioLayout = new javax.swing.GroupLayout(panelFormulario);
        panelFormulario.setLayout(panelFormularioLayout);
        panelFormularioLayout.setHorizontalGroup(
            panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormularioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(etiquetaNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(etiquetaDni, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                        .addComponent(campoDni, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(campoNombre)
                        .addComponent(campoTipoContrato)
                        .addComponent(etiquetaTipoContrato, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(etiquetaFcontrato, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(etiquetaSalario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(campoSalario, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(campoDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelFormularioLayout.setVerticalGroup(
            panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormularioLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(etiquetaDni)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(etiquetaNombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(etiquetaSalario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(etiquetaFcontrato)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(etiquetaTipoContrato)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoTipoContrato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        botonNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/add.png"))); // NOI18N
        botonNew.setText(bundle.getString("CREAR")); // NOI18N
        botonNew.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        botonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNewActionPerformed(evt);
            }
        });

        botonCanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancel.png"))); // NOI18N
        botonCanel.setText(bundle.getString("CANCELAR")); // NOI18N
        botonCanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCanelActionPerformed(evt);
            }
        });

        botonDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/gnome_edit_delete.png"))); // NOI18N
        botonDelete.setText(bundle.getString("BORRAR")); // NOI18N
        botonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDeleteActionPerformed(evt);
            }
        });

        etiquetaTitulo.setFont(new java.awt.Font("Trajan Pro", 0, 24)); // NOI18N
        etiquetaTitulo.setForeground(new java.awt.Color(153, 51, 0));
        etiquetaTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaTitulo.setText(bundle.getString("NUEVO EMPLEADO")); // NOI18N

        javax.swing.GroupLayout panelPrincipalLayout = new javax.swing.GroupLayout(panelPrincipal);
        panelPrincipal.setLayout(panelPrincipalLayout);
        panelPrincipalLayout.setHorizontalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrincipalLayout.createSequentialGroup()
                .addGroup(panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(etiquetaTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                    .addComponent(panelFormulario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(panelPrincipalLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonCanel)
                .addGap(6, 6, 6))
        );
        panelPrincipalLayout.setVerticalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrincipalLayout.createSequentialGroup()
                .addComponent(etiquetaTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelFormulario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(botonNew)
                    .addComponent(botonDelete)
                    .addComponent(botonCanel))
                .addGap(31, 31, 31))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-536)/2, (screenSize.height-465)/2, 536, 465);
    }// </editor-fold>//GEN-END:initComponents

    private void botonCanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCanelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_botonCanelActionPerformed

    private void botonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDeleteActionPerformed
        // TODO add your handling code here:
        Component[] components = panelFormulario.getComponents();
        for (int i = 0; i < components.length; i++) {
            if (components[i].getClass() == campoDni.getClass()) {
                ((JTextField) components[i]).setText("");
            }
        }
    }//GEN-LAST:event_botonDeleteActionPerformed

    private void botonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNewActionPerformed
        try {
            // TODO add your handling code here:
            Trabajadores trabajador;
            trabajador = new Trabajadores(campoDni.getText());
            trabajador.setNombre(campoNombre.getText());
            trabajador.setFechaContrato(campoDate.getDate());
            trabajador.setSalario(Float.parseFloat(campoSalario.getText()));
            trabajador.setPermiso(1);
            trabajador.setTipoContrato(campoTipoContrato.getText());

            this.modeloTrabajadores.addRow(trabajador);
            this.modeloTrabajadores.fireTableDataChanged();
            this.dispose();
        } catch (DataException | NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_botonNewActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCanel;
    private javax.swing.JButton botonDelete;
    private javax.swing.JButton botonNew;
    private com.toedter.calendar.JDateChooser campoDate;
    private javax.swing.JTextField campoDni;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoSalario;
    private javax.swing.JTextField campoTipoContrato;
    private javax.swing.JLabel etiquetaDni;
    private javax.swing.JLabel etiquetaFcontrato;
    private javax.swing.JLabel etiquetaNombre;
    private javax.swing.JLabel etiquetaSalario;
    private javax.swing.JLabel etiquetaTipoContrato;
    private javax.swing.JLabel etiquetaTitulo;
    private javax.swing.JPanel panelFormulario;
    private javax.swing.JPanel panelPrincipal;
    // End of variables declaration//GEN-END:variables
}
